# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import pymysql as pymysql
from itemadapter import ItemAdapter


class MysqlPipeline:
    def __init__(self):
        self.conn = pymysql.connect(host='127.0.0.1', user='root', password='lilishop', database='scrapy',
                                    charset='utf8')
        self.cur = self.conn.cursor()

    def process_item(self, item, spider):
        sql = """
        insert into picture(picture_url,picture_description) values( %s, %s )
        """
        self.cur.execute(sql, (item['picture_url'], item['picture_description']))
        self.conn.commit()
        return item


class Ssr1Pipeline:
    def __init__(self):
        self.conn = pymysql.connect(host='127.0.0.1', user='root', password='lilishop', database='scrapy',
                                    charset='utf8')
        self.cur = self.conn.cursor()

    def process_item(self, item, spider):
        sql = """
        insert into ssr1(film_name,film_types,film_country,film_duration,film_show_time,film_synopsis,film_director,film_score  ) values( %s, %s, %s, %s,%s, %s,%s, %s)
        """
        self.cur.execute(sql, (item['film_name'], ",".join(item['film_types']),
                               item['film_country'], item['film_duration'],
                               item['film_show_time'], item['film_synopsis'],
                               item['film_director'], item['film_score']))
        self.conn.commit()
        return item


class CotyPipeline:
    def process_item(self, item, spider):
        return item
